package com.lishe.lishe.lishe.module.me.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.lishe.lishe.R;
import com.lishe.lishe.asynchttp.LSWebHelper;
import com.lishe.lishe.lishe.base.BaseApplication;
import com.lishe.lishe.lishe.module.auth.http.AuthHttpHelper;
import com.lishe.lishe.lishe.module.auth.view.ClearEditText2;
import com.lishe.lishe.lishe.module.me.baseAc.baseAc;
import com.lishe.lishe.lishe.module.me.http.HttpUserCenter;
import com.lishe.lishe.lishe.utils.LoadingUtils;
import com.lishe.lishe.lishe.utils.ValidationUtils;

import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by zhcasecom on 2017/9/16.
 */

public class PayPassEndActivity extends baseAc implements View.OnClickListener {
    public static final String TAG = PayPassEndActivity.class.getSimpleName();

    private ClearEditText2 inputText1, inputText2;

    private Button submitBtn;

    public static void jump(Activity activity) {
        Intent intent = new Intent(activity, PayPassEndActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.paypass_end_activity);
        BaseApplication.getInstace().addActivity(this);
        initUi();
    }

    private void initUi() {
        TextView nav_title_text = findViewById(R.id.title_text_title);
        nav_title_text.setText("支付密码");
        nav_title_text.setTypeface(getTypeFaceMedium());

        Button back = findViewById(R.id.title_left_button);
        back.setOnClickListener(this);

        inputText1 = findViewById(R.id.inputText1);
        inputText1.setText("");
        inputText1.addTextChangedListener(watcher);

        inputText2 = findViewById(R.id.inputText2);
        inputText2.setText("");
        inputText2.addTextChangedListener(watcher);

        submitBtn = findViewById(R.id.submitBtn_Confirm);
        submitBtn.setOnClickListener(this);
        submitBtn.setEnabled(false);
        submitBtn.setBackgroundResource(R.drawable.shape_submit_pwd_change_bg_disable);


    }

    @Override
    public void onClick(View view) {

        if (view.getId() == R.id.title_left_button) {

            BaseApplication.getInstace().removeActivity(this);
            finish();
        } else if (view.getId() == R.id.redirectBtn1) {


        } else if (view.getId() == R.id.submitBtn_Confirm) {

            final String password1 = inputText1.getText().toString().trim();
            final String password2 = inputText2.getText().toString().trim();
            HashMap params = new HashMap();

            Log.e(TAG, password1 + "  " + password2 + "  " + AuthHttpHelper.phoneOfChangePayPwd);

            params.put("mobile", AuthHttpHelper.phoneOfChangePayPwd);
            params.put("newPayPassword", password1);
            params.put("affirmPayPassword", password2);
            LoadingUtils.createLoadingDialog(this, "请稍等...",true);
            HttpUserCenter.doSetPayPasswordInSetting(this, params, new LSWebHelper.WebCallBack() {
                @Override
                public void onSuccess(JSONObject dataJson, String dataString, String message) {
                    AuthHttpHelper.phoneOfChangePayPwd = null;
                    LoadingUtils.closeDialog();
//                    MainActivity.jump(PayPassEndActivity.this);
                    BaseApplication.getInstace().finishAllActivities();
                }

                @Override
                public void onFailure(String message) {
                    LoadingUtils.closeDialog();
                    Toast.makeText(PayPassEndActivity.this, message, Toast.LENGTH_SHORT).show();
                }
            });


        }
    }

    private TextWatcher watcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            String input1 = inputText1.getText().toString();
            String input2 = inputText2.getText().toString();
            if(!ValidationUtils.isLimitPass(input1)){
                Toast.makeText(PayPassEndActivity.this, R.string.LimitPass, Toast.LENGTH_SHORT).show();
                inputText1.setFocusable(true);
            }
            if(!ValidationUtils.isLimitPass(input2)){
                Toast.makeText(PayPassEndActivity.this, R.string.LimitPass, Toast.LENGTH_SHORT).show();
                inputText2.setFocusable(true);
            }
            if (input1.length() == input2.length() && !input1.equals(input2)) {
                Toast.makeText(PayPassEndActivity.this, R.string.paypass_set_fail1, Toast.LENGTH_SHORT).show();
            }
            if (input1.length() < 6 || input2.length() < 6 || !input1.equals(input2)) {
                submitBtn.setEnabled(false);
                submitBtn.setBackgroundResource(R.drawable.shape_submit_pwd_change_bg_disable);
            } else {
                submitBtn.setEnabled(true);
                submitBtn.setBackgroundResource(R.drawable.shape_submit_pwd_change_bg);
            }

        }
    };

}
